﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;

namespace OT.Areas.Admin.Controllers
{
    public class ErrorLogController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<ErrorLogs> log = ((from c in OTDb.ErrorLogs
                        select new ErrorLogs
                        {
                            ErrorID = c.ErrorID,
                            ErrorTitle = c.ErrorTitle,
                            ErrorCode = c.ErrorCode,
                            Description = c.Description,
                            EmailAddress = c.EmailAddress,
                            NatureOfProblem = c.NatureOfProblem,
                            CreatedDate=(DateTime)c.CreatedDate,
                            IsSolved = c.IsSolved
                                }).OrderBy(o => o.CreatedDate).ToList<ErrorLogs>());

            if (!string.IsNullOrEmpty(searchString))
                log = log.Where(r => r.ErrorCode.ToLower().Contains(searchString.ToLower())).ToList();
            int recount = log.Count;
            log = log.Skip<ErrorLogs>(pgindex * (pageNumber - 1))
                .Take<ErrorLogs>(pgindex).ToList<ErrorLogs>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = log,
                RecordCount = recount
            });
        }
        #endregion

        #region Delete
        public ActionResult Delete(int ErrorID)
        {
            ErrorLogs err = null;
            try
            {
                var categry = OTDb.ErrorLogs.Where(r => r.ErrorID == ErrorID).SingleOrDefault();
                if (categry != null)
                {
                    err = new ErrorLogs();
                    err.ErrorCode = categry.ErrorCode;
                    err.ErrorID = categry.ErrorID;
                    err.ErrorTitle = categry.ErrorTitle;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(err);
        }

        [HttpPost]
        public string Delete(int ErrorID, string IsDelete)
        {
            string result = "";
            try
            {
                var City = OTDb.ErrorLogs.Where(r => r.ErrorID == ErrorID).SingleOrDefault();
                if (City != null)
                {
                    OTDb.ErrorLogs.Remove(City);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}


